<template>
  <div class="top-right-btn" :style="style">
    <el-row>
      <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top" v-if="search">
        <el-button size="small" circle icon="Search" @click="toggleSearch()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button size="small" circle icon="Refresh" @click="refresh()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="显隐列" placement="top" v-if="columns.length > 0">
        <el-button size="small" circle icon="Menu" @click="showColumn()" />
      </el-tooltip>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-transfer :titles="['显示', '隐藏']" v-model="value" :data="columns" @change="dataChange"></el-transfer>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
const defProps = withDefaults(defineProps<{
  showSearch: boolean,
  columns: any[],
  search: boolean,
  gutter: number
}>(), {
  showSearch: true,
  search: true,
  columns: () => [],
  gutter: 10,
})

// 显隐数据
const value = ref<any>([]);
// 弹出层标题
const title = ref("显示/隐藏");
// 是否显示弹出层
const open = ref(false);

const style = computed(() => {
  const ret: any = {};
  if (defProps.gutter) {
    ret.marginRight = `${defProps.gutter / 2}px`;
  }
  return ret;
})

// 显隐列初始默认隐藏列
for (let item in defProps.columns) {
  if (defProps.columns[item].visible === false) {
    value.push(parseInt(item));
  }
}

const emits = defineEmits<{
  (e: 'update:showSearch', isShowSearch: boolean): void,
  (e: 'queryTable'): void,
}>();

// 是否展示搜索
const toggleSearch = () => {
  console.log(!defProps.showSearch)
  emits("update:showSearch", !defProps.showSearch);
}

// 刷新
const refresh = () => {
  emits("queryTable");
}

// 右侧列表元素变化
const dataChange = (data: any) => {
  for (let item in defProps.columns) {
    const key = defProps.columns[item].key;
    defProps.columns[item].visible = !data.includes(key);
  }
}

// 打开显隐列dialog
const showColumn = () => {
  open.value = true;
}

</script>

<style lang="scss" scoped>
:deep(.el-transfer__button) {
  border-radius: 50%;
  padding: 12px;
  display: block;
  margin-left: 0px;
}

:deep(.el-transfer__button:first-child) {
  margin-bottom: 10px;
}
</style>
